package com.ebt.m.policy.presenter;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.RecyclerFootManger;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.data.rxModel.apibean.ApiPolicyProducts;
import com.ebt.m.policy.bean.BrandProductTitle;
import com.ebt.m.utils.ConfigData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Damon.Han
 */
public class BrandsProductsPresenter extends BaseListPresenter<BaseListContract.IView> {
    private int currentPage = 1;
    public static final int TITLE_VIEW_YTPE = 0;
    public static final int DATA_VIEW_TYPE = 1;


    public BrandsProductsPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        currentPage = 1;
        EBTAPI ebtapi = AppInitService.getEbtApi();
        Map<String, String> params = null;
        if (args[0] == null) {
            params = new HashMap<>();
        } else {
            params = (Map<String, String>) args[0];
        }
        params.put("page", currentPage + "");
        params.put("pageSize", ConfigData.PAGE_COUNT_LIMIT + "");
        ebtapi.getBrandsProducts(params)
                .compose(RxUtil.rxTransformer(iView))
                .filter(getFilterForFootviewStatus(true))
                .subscribe(
                        response -> {
                            iView.updateData(createSectionItem(response.getData()));
                            iView.updateCount(response.getPaging());
                        }, throwable -> {
                            throwable.printStackTrace();
                            iView.errorData();
                        });
    }

    @Override
    public List<SectionItem> createSectionItem(List list) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (list == null) {
            return sections;
        }
        ApiPolicyProducts policyProducts = null;
        for (int i = 0; i < list.size(); i++) {
            policyProducts = (ApiPolicyProducts) list.get(i);
            BrandProductTitle title = new BrandProductTitle();
            title.setBrandId(policyProducts.getBrandId());
            title.setMinLogo(policyProducts.getMinLogo());
            title.setName(policyProducts.getName());
            title.setProductCount(policyProducts.getProductCount());
            title.setShortName(policyProducts.getShortName());
            title.setProductItems(policyProducts.getProductItems());
            title.setBrandName(policyProducts.getName());
            sections.add(new SectionItem(TITLE_VIEW_YTPE, title));
            if (policyProducts.getProductItems() != null) {
                int index = 2;
                if (policyProducts.getProductItems().size() < index) {
                    index = policyProducts.getProductItems().size();
                }
                for (int j = 0; j < index; j++) {
                    sections.add(new SectionItem(DATA_VIEW_TYPE, policyProducts.getProductItems().get(j)));
                }
            }
        }
        return sections;
    }

    @Override
    public void refresh() {
        Object arg = null;
        loadNew(arg);
    }

    @Override
    public void loadNext(Object... args) {
        EBTAPI searchApi = AppInitService.getEbtApi();
        Map<String, String> params = null;
        if (args[0] == null) {
            params = new HashMap<>();
        } else {
            params = (Map<String, String>) args[0];
        }
        params.put("page", currentPage + 1 + "");
        params.put("pageSize", ConfigData.PAGE_COUNT_LIMIT + "");
        searchApi.getBrandsProducts(params)
                .filter(getFilterForFootviewStatus(false))
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(
                        response -> {
                            currentPage++;
                            iView.updateDataAdd(createSectionItem(response.getData()));
                        }, throwable -> {
                            throwable.printStackTrace();
                            iView.setFootStatus(RecyclerFootManger.STATUS_ERROR, true);
                        });
    }

}
